﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StatusPlan3
{
    /// <summary>
    /// Interaction logic for Window_ProjectGroup.xaml
    /// </summary>
    public partial class Window_ProjectGroup : Window
    {
        ProjectGroup projectGroup;
        Boolean isNew = false;

        public Window_ProjectGroup()
        {
            InitializeComponent();
        }

        public void showNew()
        {
            isNew = true;
            TextBlock_Title.Text = "Stovna Verkætlanarbók";
            OkButto.Content = "Stovna";
            projectGroup = new ProjectGroup();

            this.ShowDialog();
        }

        public void showEdit(ProjectGroup pg)
        {
            projectGroup = pg;
            TextBlock_Title.Text = "Broyt Verkætlanarbók";
            OkButto.Content = "Broyt";

            NameTextBox.Text = projectGroup.name;
            StartDateTextBox.SelectedDate = projectGroup.startDate;
            EstimatedEndDateTextBox.SelectedDate = projectGroup.planedEndDate;
            EndDateTextBox.SelectedDate = projectGroup.endDate;
            ActiveCheckBox.IsChecked = projectGroup.isActive;

            this.ShowDialog();
        }

        private Boolean validateFields()
        {
            Boolean ok = true;
            String errorString = "";

            if (NameTextBox.Text.Trim().Length == 0)
            {
                ok = false;
                errorString = "Navn feltið er kravt";
            }

            if (!ok)
                MessageBox.Show(errorString, "Feilur", MessageBoxButton.OK, MessageBoxImage.Error);

            return ok;
        }

        private void OkButto_Click(object sender, RoutedEventArgs e)
        {
            if (validateFields())
            {
                if (isNew)
                {
                    projectGroup.name = NameTextBox.Text;
                    projectGroup.startDate = StartDateTextBox.SelectedDate;
                    projectGroup.planedEndDate = EstimatedEndDateTextBox.SelectedDate;
                    projectGroup.endDate = EndDateTextBox.SelectedDate;
                    projectGroup.isActive = ActiveCheckBox.IsChecked.Value;
                    projectGroup.createdBy = StatusPlanData.currentUser;
                    projectGroup.createdDate = DateTime.Now;

                    StatusPlanData.statusPlanHandler.saveProjectGroup(projectGroup);

                    if (MessageBox.Show("Vilt tú leggja limir aftrat verkætlanrbólki nú?", "Limir", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        Window_SelectUsers wsu = new Window_SelectUsers();
                        wsu.setData(projectGroup);
                    }
                }
                else
                {
                    projectGroup.name = NameTextBox.Text;
                    projectGroup.startDate = StartDateTextBox.SelectedDate;
                    projectGroup.planedEndDate = EstimatedEndDateTextBox.SelectedDate;
                    projectGroup.endDate = EndDateTextBox.SelectedDate;
                    projectGroup.isActive = ActiveCheckBox.IsChecked.Value;

                    StatusPlanData.statusPlanHandler.saveProjectGroup(projectGroup);
                }
                this.Close();
            }
        }

        private void CancelButto_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
